//
//  SLDownloadMissionManager.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/18.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLDownloadMissionManager.h"

#import <AFNetworking/AFNetworking.h>
#import <YYKit/YYKit.h>

#import "SLLogger.h"
#import "SLDownloadDataManager.h"

@interface SLDownloadMissionManager ()

@property (nonatomic, strong) NSMutableArray *dataList;

@end

@implementation SLDownloadMissionManager

+ (instancetype)shareInstance {
    static SLDownloadMissionManager *shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (shareInstance == nil) {
            shareInstance = [[SLDownloadMissionManager alloc] init];
        }
    });
    
    return shareInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initManager];
    }
    return self;
}

- (void)initManager {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] addObserverBlockForKeyPath:@"networkReachabilityStatus" block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
        
        if (![oldVal isKindOfClass:[NSNumber class]] || ![newVal isKindOfClass:[NSNumber class]]) {
            return;
        }
        
        NSNumber *oldValNum = oldVal;
        NSNumber *newValNum = newVal;
        NSInteger oldValInt = [oldValNum integerValue];
        NSInteger newValInt = [newValNum integerValue];
        
        if (oldValInt < 0 || oldValInt > 2 || newValInt < 0 || newValInt > 2) {
            return;
        }
        
        AFNetworkReachabilityStatus oldStatus = oldValInt;
        AFNetworkReachabilityStatus newStatus = newValInt;
        
        switch (newStatus) {
            case AFNetworkReachabilityStatusNotReachable:{
                if (oldStatus != AFNetworkReachabilityStatusUnknown && oldStatus != AFNetworkReachabilityStatusNotReachable) {
                    [self pauseAllTasksToDownloadStateEnum:SLDownloadStatePaused];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                if (oldStatus == AFNetworkReachabilityStatusNotReachable) {
                    [self startAllTasks];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                if (oldStatus == AFNetworkReachabilityStatusNotReachable) {
                    [self startAllTasks];
                }
                break;
            }
            case AFNetworkReachabilityStatusUnknown:{
                NSLog(@"网络状态检测出现未知网络状态");
                break;
            }
            default:{
                NSLog(@"网络状态检测出现未知网络状态");
                break;
            }
        }
        if (newStatus == AFNetworkReachabilityStatusNotReachable) {
            
        }
        
        
        [[SLLogger sharedInstance] addLog:[NSString stringWithFormat:@"obj: %@, oldVal:%@, newVal:%@", obj, oldVal, newVal]];
        NSLog(@"obj: %@, oldVal:%@, newVal:%@", obj, oldVal, newVal);
    }];
}

- (NSArray<SLDownloadFileModel *> *)queryAllData {
    if ([self.dataList isKindOfClass:[NSArray class]] && self.dataList.count > 0) {
        
    }
    else {
        self.dataList = [[[SLDownloadDataManager shareInstance] queryAllDatas] mutableCopy];
    }
    
    if (!self.dataList) {
        self.dataList = [NSMutableArray new];
    }
    
    return self.dataList;
}

- (void)startAllTasks {
    for (SLDownloadFileModel *fileModel in self.dataList) {
        if ([fileModel isKindOfClass:[SLDownloadFileModel class]]) {
            if (fileModel.downloadStateEnum != SLDownloadStateDownloading) {
                [[SLDownloadTaskManager shareInstance] startDownloadWithFileModel:fileModel];
            }
        }
    }
}

- (void)pauseDownloadWithFileModel:(SLDownloadFileModel *)fileModel toDownloadStateEnum:(SLDownloadState)downloadStateEnum {
    [[SLDownloadTaskManager shareInstance] pauseDownloadWithFileModel:fileModel toDownloadStateEnum:downloadStateEnum];
}

- (void)pauseAllTasksToDownloadStateEnum:(SLDownloadState)downloadStateEnum {
    for (SLDownloadFileModel *fileModel in self.dataList) {
        if ([fileModel isKindOfClass:[SLDownloadFileModel class]]) {
            if (fileModel.downloadStateEnum == SLDownloadStateDownloading) {
                [[SLDownloadTaskManager shareInstance] pauseDownloadWithFileModel:fileModel toDownloadStateEnum:downloadStateEnum];
            }
        }
    }
}

@end
